package keter.framework.web.security;

import java.awt.image.BufferedImage;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import keter.framework.web.controller.CaptchaController;

import com.github.napp.kaptcha.Kaptcha;
import com.github.napp.kaptcha.KaptchaTextCreator;

/**
     * @author abledea
     * @since Oct 26, 2014
     */
    public final class CaptchaCache implements Serializable{
    	
        /**long serialVersionUID: TODO属性声明*/
	    private static final long serialVersionUID = 1L;

        private static final int DEFAULT_KAPTCHA_LEN = 4;

        final public String actualText = KaptchaTextCreator.getText(DEFAULT_KAPTCHA_LEN);

        final public BufferedImage image = new Kaptcha().createImage(actualText);
        
        public static boolean checkCode(HttpServletRequest request, String captchaGuess){
            String code = new CaptchaController().getCaptchaCode(request.getSession());
        	 if (code == null || !code.equals(captchaGuess)) {
        		 return false;
             } else {
                 //验证码输入正确后即对其进行废弃处理
                 new CaptchaController().dropCaptcha(request.getSession());
                 return true;
             }
        }
    }